package com.yitiao.controller;

import com.yitiao.annotation.IgnoreResponseAdvice;
import com.yitiao.dubbo.service.InfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("/rpc")
@IgnoreResponseAdvice
@Slf4j
public class TestRpcController {
    @DubboReference
    private InfoService infoService;

    @GetMapping("/info")
    public String getInfo(){
       return infoService.getInfo();
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Resource
    private  RestTemplate restTemplate;

    @GetMapping(value = "/echo/{str}")
    public String echo(@PathVariable String str) throws InterruptedException {
        log.info("开始调用日志");
        return restTemplate.getForObject("http://dubbo-provider/echo/" + str, String.class);
    }

    @GetMapping(value = "/testServiceRespTimeRule")
    public String testServiceRespTimeRule() throws InterruptedException {
        log.info("测试告警指标:{}", "testServiceRespTimeRule");
        Thread.sleep(1000);
        return "success";
    }
}
